package ca.krakenpower.kvoxels;

public class Vector2 {
	
	public static final Vector2 ZERO = 	get(0, 0);
	public static final Vector2 UP =	get(0, 1);
	public static final Vector2 DOWN =	get(0, -1);
	public static final Vector2 RIGHT = get(1, 0);
	public static final Vector2 LEFT =	get(-1, 0);
	
	public final double x, y;
	
	private Vector2(double x, double y) {
		this.x = x;
		this.y = y;
	}
	
	/**
	 * @return a vector with the same magnitude as this vector but opposite direction.
	 */
	public Vector2 negate() {
		return get(0 - x, 0 - y);
	}
	
	/**
	 * @param b
	 * @return a vector who's components are the sums of the respective components of this vector and b.
	 */
	public Vector2 sum(Vector2 b) {
		if (b == null) {
			throw new IllegalArgumentException("null vector given as summand");
		}
		
		return get(x + b.x, y + b.y);
	}
	
	/**
	 * @param b
	 * @return the scalar dot product of this vector and vector b.
	 */
	public double dot(Vector2 b) {
		if (b == null) {
			throw new IllegalArgumentException("null vector given as argument of dot product");
		}
		
		return (this.x * b.x) + (this.y * b.y); 
	}
	
	/**
	 * @return the length (magnitude) of this vector.
	 */
	public double length() {
		return Math.sqrt((x * x) + (y * y));
	}
	
	/**
	 * @param b
	 * @return the angle (in radians) formed by this vector and vector b.
	 */
	public double angle(Vector2 b) {
		return Math.acos(this.dot(b) / (this.length() * b.length()));
	}
	
	/**
	 * @param x
	 * @param y
	 * @param z
	 * @return a new vector with the supplied components
	 */
	public static Vector2 get(double x, double y) {
		return new Vector2(x, y);
	}

}
